﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ALiteTeam.PackageTracer.Service
{
    public class EmsFilter : BaseFilter
    {
        private static readonly string REGX = "<tr>\\s+<td align=\"right\"( class=\"backcolor\")?>\\s+([^<]+)\\s+</td>\\s+<td class=\"(backcolor )?middle_border\"( align=\"center\")?>(\\s+)?(\\w+)(\\s+)?</td>\\s+<td (align=\"center\" class=\"backcolor\")?>\\s+([^<]+)</td>";
        private static readonly string REGX_SUMMARY = "<span[^>]+>([^<]+)</span>\\s+<span[^>]+>([^<]+)</span>\\s+<span[^>]+>([^<]+)</span>\\s+(<span[^>]+>([^<]+)</span>\\s+<br />)?";

        public override QueryResult GetSummary(string content)
        {
            QueryResult result;

            _Content = content;
            Regex regex = new Regex(REGX_SUMMARY, RegexOptions.Compiled | RegexOptions.IgnoreCase);
            Match match = regex.Match(content);

            if (match.Success == true)
            {
                var str1 = match.Groups[1].Value.Trim();
                var dateTime = match.Groups[2].Value.Replace("&nbsp;", " ").Trim();
                var unit = match.Groups[3].Value.Trim();
                var action = match.Groups[4].Value.Trim();

                // 当最后投递成功时，Action不为空；否则直接取Unit的值
                string message = string.Empty;
                if (string.IsNullOrEmpty(action) == true)
                {
                    message = unit;
                }
                else
                {
                    message = string.Format("{0}: {1}", unit, action);
                }

                result = new QueryResult()
                {
                    OrderNo = _OrderNo,
                    Status = 1,
                    Summary = "ok",
                    Details = new List<QueryResult.Detail>
                    {
                        new QueryResult.Detail()
                        {
                             FTime = DateTime.Parse(dateTime),
                             Context = message
                        }
                    }
                };
            }
            else
            {
                result = new QueryResult()
                {
                    OrderNo = _OrderNo,
                    Status = 0,
                    Summary = "查询失败，可能是EMS官网服务中止"
                };
            }

            return result;
        }

        public override QueryResult GetDetails(string content)
        {
            QueryResult result;

            _Content = content;
            Regex regex = new Regex(REGX, RegexOptions.Compiled | RegexOptions.IgnoreCase);
            MatchCollection col = regex.Matches(content);

            if (col.Count > 0)
            {
                result = new QueryResult()
                {
                    OrderNo = _OrderNo,
                    Status = 1,
                    Summary = "ok",
                    Details = new List<QueryResult.Detail>()
                };

                foreach (Match match in col)
                {
                    var dateTime = match.Groups[2].Value.Replace("&nbsp;", " ").Replace("   ", " ").Trim();
                    var unit = match.Groups[6].Value.Trim();
                    var action = match.Groups[9].Value.Trim();

                    QueryResult.Detail item = new QueryResult.Detail()
                    {
                        FTime = DateTime.Parse(dateTime),
                        Context = string.Format("{0}: {1}", unit, action)
                    };

                    result.Details.Add(item);
                }
            }
            else
            {
                result = new QueryResult()
                {
                    OrderNo = _OrderNo,
                    Status = 0,
                    Summary = "查询失败，可能是EMS官网服务中止"
                };
            }

            return result;
        }
    }
}
